package com.abel.bigwater.dataserver.model.kming

import io.netty.buffer.ByteBuf
import java.util.*

class DeviceStatus : TermData(KmingDataType.TermDummy, 2) {
    var back: BitSet? = null

    var lowVoltTerm10: Boolean = false
    var moveAbnormal11: Boolean = false
    var shortCircuit12: Boolean = false
    var openCirCuit13: Boolean = false

    var lowVoltMeter20: Boolean = false
    var emptyWarn25: Boolean = false
    var converterFault26: Boolean = false
    var sensorFault27: Boolean = false

    override fun bufLength() = 2

    override fun parseBuf(buf: ByteBuf): Boolean {
        BitSet.valueOf(ByteArray(2).also { buf.readBytes(it) }).apply {
            back = this

            lowVoltTerm10 = get(0)
            moveAbnormal11 = get(1)
            shortCircuit12 = get(2)
            openCirCuit13 = get(3)

            lowVoltMeter20 = get(8)
            emptyWarn25 = get(13)
            converterFault26 = get(14)
            sensorFault27 = get(15)
        }

        return true
    }

    override fun pack(buf: ByteBuf) {
        BitSet(16).apply {
            set(0, lowVoltTerm10)
            set(1, moveAbnormal11)
            set(2, shortCircuit12)
            set(3, openCirCuit13)

            set(8, lowVoltMeter20)
            set(13, emptyWarn25)
            set(14, converterFault26)
            set(15, sensorFault27)

            val ba = this.toByteArray()
            val zeroArray = byteArrayOf(0, 0)
            buf.writeBytes(if (ba.isEmpty()) zeroArray else ba.plus(zeroArray).copyOf(2))
        }
    }
}